{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Copy of NAMD_MD0.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "accelerator": "GPU",
    "widgets": {
      "application/vnd.jupyter.widget-state+json": {
        "ff68e344e05449dbb4784f425e0c6594": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "HBoxModel",
          "model_module_version": "1.5.0",
          "state": {
            "_view_name": "HBoxView",
            "_dom_classes": [],
            "_model_name": "HBoxModel",
            "_view_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_view_count": null,
            "_view_module_version": "1.5.0",
            "box_style": "",
            "layout": "IPY_MODEL_5614ed21ad704c71b682ebf7ed093116",
            "_model_module": "@jupyter-widgets/controls",
            "children": [
              "IPY_MODEL_4b00d84c07a844ddad7792b77caced95",
              "IPY_MODEL_de2a82e34e5a4887aca6f200785b2a39",
              "IPY_MODEL_b09278c4bcb44d11b6ba2a74fe7104fc"
            ]
          }
        },
        "5614ed21ad704c71b682ebf7ed093116": {
          "model_module": "@jupyter-widgets/base",
          "model_name": "LayoutModel",
          "model_module_version": "1.2.0",
          "state": {
            "_view_name": "LayoutView",
            "grid_template_rows": null,
            "right": null,
            "justify_content": null,
            "_view_module": "@jupyter-widgets/base",
            "overflow": null,
            "_model_module_version": "1.2.0",
            "_view_count": null,
            "flex_flow": null,
            "width": null,
            "min_width": null,
            "border": null,
            "align_items": null,
            "bottom": null,
            "_model_module": "@jupyter-widgets/base",
            "top": null,
            "grid_column": null,
            "overflow_y": null,
            "overflow_x": null,
            "grid_auto_flow": null,
            "grid_area": null,
            "grid_template_columns": null,
            "flex": null,
            "_model_name": "LayoutModel",
            "justify_items": null,
            "grid_row": null,
            "max_height": null,
            "align_content": null,
            "visibility": null,
            "align_self": null,
            "height": null,
            "min_height": null,
            "padding": null,
            "grid_auto_rows": null,
            "grid_gap": null,
            "max_width": null,
            "order": null,
            "_view_module_version": "1.2.0",
            "grid_template_areas": null,
            "object_position": null,
            "object_fit": null,
            "grid_auto_columns": null,
            "margin": null,
            "display": null,
            "left": null
          }
        },
        "4b00d84c07a844ddad7792b77caced95": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "HTMLModel",
          "model_module_version": "1.5.0",
          "state": {
            "_view_name": "HTMLView",
            "style": "IPY_MODEL_10193f088e9240a796579932a0279b03",
            "_dom_classes": [],
            "description": "",
            "_model_name": "HTMLModel",
            "placeholder": "​",
            "_view_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "value": "100%",
            "_view_count": null,
            "_view_module_version": "1.5.0",
            "description_tooltip": null,
            "_model_module": "@jupyter-widgets/controls",
            "layout": "IPY_MODEL_3ab0d0129ca64d76a2c93c7b1e59b292"
          }
        },
        "de2a82e34e5a4887aca6f200785b2a39": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "FloatProgressModel",
          "model_module_version": "1.5.0",
          "state": {
            "_view_name": "ProgressView",
            "style": "IPY_MODEL_fd2af40e2ca2465ea4252203348584a3",
            "_dom_classes": [],
            "description": "",
            "_model_name": "FloatProgressModel",
            "bar_style": "success",
            "max": 100,
            "_view_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "value": 100,
            "_view_count": null,
            "_view_module_version": "1.5.0",
            "orientation": "horizontal",
            "min": 0,
            "description_tooltip": null,
            "_model_module": "@jupyter-widgets/controls",
            "layout": "IPY_MODEL_7c74f1abdeaa46baa8ef759c34972cb2"
          }
        },
        "b09278c4bcb44d11b6ba2a74fe7104fc": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "HTMLModel",
          "model_module_version": "1.5.0",
          "state": {
            "_view_name": "HTMLView",
            "style": "IPY_MODEL_3c137f0764824327acef0cb2c854bab5",
            "_dom_classes": [],
            "description": "",
            "_model_name": "HTMLModel",
            "placeholder": "​",
            "_view_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "value": " 100/100 [elapsed: 04:48 remaining: 00:00]",
            "_view_count": null,
            "_view_module_version": "1.5.0",
            "description_tooltip": null,
            "_model_module": "@jupyter-widgets/controls",
            "layout": "IPY_MODEL_e2db23fe40d84b81abf847de6adb36bf"
          }
        },
        "10193f088e9240a796579932a0279b03": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "DescriptionStyleModel",
          "model_module_version": "1.5.0",
          "state": {
            "_view_name": "StyleView",
            "_model_name": "DescriptionStyleModel",
            "description_width": "",
            "_view_module": "@jupyter-widgets/base",
            "_model_module_version": "1.5.0",
            "_view_count": null,
            "_view_module_version": "1.2.0",
            "_model_module": "@jupyter-widgets/controls"
          }
        },
        "3ab0d0129ca64d76a2c93c7b1e59b292": {
          "model_module": "@jupyter-widgets/base",
          "model_name": "LayoutModel",
          "model_module_version": "1.2.0",
          "state": {
            "_view_name": "LayoutView",
            "grid_template_rows": null,
            "right": null,
            "justify_content": null,
            "_view_module": "@jupyter-widgets/base",
            "overflow": null,
            "_model_module_version": "1.2.0",
            "_view_count": null,
            "flex_flow": null,
            "width": null,
            "min_width": null,
            "border": null,
            "align_items": null,
            "bottom": null,
            "_model_module": "@jupyter-widgets/base",
            "top": null,
            "grid_column": null,
            "overflow_y": null,
            "overflow_x": null,
            "grid_auto_flow": null,
            "grid_area": null,
            "grid_template_columns": null,
            "flex": null,
            "_model_name": "LayoutModel",
            "justify_items": null,
            "grid_row": null,
            "max_height": null,
            "align_content": null,
            "visibility": null,
            "align_self": null,
            "height": null,
            "min_height": null,
            "padding": null,
            "grid_auto_rows": null,
            "grid_gap": null,
            "max_width": null,
            "order": null,
            "_view_module_version": "1.2.0",
            "grid_template_areas": null,
            "object_position": null,
            "object_fit": null,
            "grid_auto_columns": null,
            "margin": null,
            "display": null,
            "left": null
          }
        },
        "fd2af40e2ca2465ea4252203348584a3": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "ProgressStyleModel",
          "model_module_version": "1.5.0",
          "state": {
            "_view_name": "StyleView",
            "_model_name": "ProgressStyleModel",
            "description_width": "",
            "_view_module": "@jupyter-widgets/base",
            "_model_module_version": "1.5.0",
            "_view_count": null,
            "_view_module_version": "1.2.0",
            "bar_color": null,
            "_model_module": "@jupyter-widgets/controls"
          }
        },
        "7c74f1abdeaa46baa8ef759c34972cb2": {
          "model_module": "@jupyter-widgets/base",
          "model_name": "LayoutModel",
          "model_module_version": "1.2.0",
          "state": {
            "_view_name": "LayoutView",
            "grid_template_rows": null,
            "right": null,
            "justify_content": null,
            "_view_module": "@jupyter-widgets/base",
            "overflow": null,
            "_model_module_version": "1.2.0",
            "_view_count": null,
            "flex_flow": null,
            "width": null,
            "min_width": null,
            "border": null,
            "align_items": null,
            "bottom": null,
            "_model_module": "@jupyter-widgets/base",
            "top": null,
            "grid_column": null,
            "overflow_y": null,
            "overflow_x": null,
            "grid_auto_flow": null,
            "grid_area": null,
            "grid_template_columns": null,
            "flex": null,
            "_model_name": "LayoutModel",
            "justify_items": null,
            "grid_row": null,
            "max_height": null,
            "align_content": null,
            "visibility": null,
            "align_self": null,
            "height": null,
            "min_height": null,
            "padding": null,
            "grid_auto_rows": null,
            "grid_gap": null,
            "max_width": null,
            "order": null,
            "_view_module_version": "1.2.0",
            "grid_template_areas": null,
            "object_position": null,
            "object_fit": null,
            "grid_auto_columns": null,
            "margin": null,
            "display": null,
            "left": null
          }
        },
        "3c137f0764824327acef0cb2c854bab5": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "DescriptionStyleModel",
          "model_module_version": "1.5.0",
          "state": {
            "_view_name": "StyleView",
            "_model_name": "DescriptionStyleModel",
            "description_width": "",
            "_view_module": "@jupyter-widgets/base",
            "_model_module_version": "1.5.0",
            "_view_count": null,
            "_view_module_version": "1.2.0",
            "_model_module": "@jupyter-widgets/controls"
          }
        },
        "e2db23fe40d84b81abf847de6adb36bf": {
          "model_module": "@jupyter-widgets/base",
          "model_name": "LayoutModel",
          "model_module_version": "1.2.0",
          "state": {
            "_view_name": "LayoutView",
            "grid_template_rows": null,
            "right": null,
            "justify_content": null,
            "_view_module": "@jupyter-widgets/base",
            "overflow": null,
            "_model_module_version": "1.2.0",
            "_view_count": null,
            "flex_flow": null,
            "width": null,
            "min_width": null,
            "border": null,
            "align_items": null,
            "bottom": null,
            "_model_module": "@jupyter-widgets/base",
            "top": null,
            "grid_column": null,
            "overflow_y": null,
            "overflow_x": null,
            "grid_auto_flow": null,
            "grid_area": null,
            "grid_template_columns": null,
            "flex": null,
            "_model_name": "LayoutModel",
            "justify_items": null,
            "grid_row": null,
            "max_height": null,
            "align_content": null,
            "visibility": null,
            "align_self": null,
            "height": null,
            "min_height": null,
            "padding": null,
            "grid_auto_rows": null,
            "grid_gap": null,
            "max_width": null,
            "order": null,
            "_view_module_version": "1.2.0",
            "grid_template_areas": null,
            "object_position": null,
            "object_fit": null,
            "grid_auto_columns": null,
            "margin": null,
            "display": null,
            "left": null
          }
        }
      }
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "y1aSqf6lkKXd"
      },
      "source": [
        "⚠️⚠️⚠️⚠️This notebook needs swithing to GPU.\n",
        "This note book is only suit for protein with a single chain for the moment."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "VVaXwqhBl8ZC"
      },
      "source": [
        "# 👋 Hi there.你好👋\n",
        "This is for a general simulation for a RCSB PDB structures with ligand bound. This notebook is only suggested for a single-chained protein ligand system for the moment.\n",
        "\n",
        "This notebook take http://zarbi.chem.yale.edu/ligpargen/namd_tutorial.html as a reference. This notebook is wrritten by quantaosun@gmail.com in Shanghai, China, 2021. \n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "rW1iQE26-ao4"
      },
      "source": [
        "-------------------------------------------------------------------------\n",
        "When you see a ⚠️ or ⚠️⚠️， that is where eithter need you to do some extra work out of this notebook  or a reminder it is easy to go wrong.\n",
        "-------------------------------------------------------------------------\n",
        "\n",
        "---\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "MgB8tSXI_NwQ"
      },
      "source": [
        "# The tutorial uses an example structure of 2-propylphenol in complex with T4 lysozyme L99A/M102Q (PDB ID: 3HTB.pdb)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "aTCtNy4mnN_A"
      },
      "source": [
        "**The overall procedure is as below**\n",
        "0.   PDB structure check and fix with third-party tools or web server.\n",
        "1.   Installation of Anaconda,pymol, vmd, and openmm, pdbfixer\n",
        "2.   PDB ID provided by user\n",
        "3.   Separation of ligand and protein\n",
        "4.   Topology generation for ligand using LigParGen web server\n",
        "5.   Combine new ligand pdb and original protein, mannually renumber ligand atom\n",
        "6.   Generation of ionized.pdb and ionized.psf by VMD, with Charmm force field.\n",
        "7.   Generation of conf file for NAMD simulation. \n",
        "8.   GPU accelerted NAMD simulation and copy simualtion data to google drive.\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "6aJ32tCkthTM",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "cellView": "form",
        "outputId": "ef54a961-f565-4b33-82b1-fdd6b31cb42e"
      },
      "source": [
        "#@title 👋Mount to Goole Drive\n",
        "from google.colab import drive\n",
        "drive.mount('/content/drive')"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Mounted at /content/drive\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 49,
          "referenced_widgets": [
            "ff68e344e05449dbb4784f425e0c6594",
            "5614ed21ad704c71b682ebf7ed093116",
            "4b00d84c07a844ddad7792b77caced95",
            "de2a82e34e5a4887aca6f200785b2a39",
            "b09278c4bcb44d11b6ba2a74fe7104fc",
            "10193f088e9240a796579932a0279b03",
            "3ab0d0129ca64d76a2c93c7b1e59b292",
            "fd2af40e2ca2465ea4252203348584a3",
            "7c74f1abdeaa46baa8ef759c34972cb2",
            "3c137f0764824327acef0cb2c854bab5",
            "e2db23fe40d84b81abf847de6adb36bf"
          ]
        },
        "id": "5rPSPjB5mYos",
        "cellView": "form",
        "outputId": "fa685ec2-6e6e-46c3-e451-c8c1d4b05221"
      },
      "source": [
        "#@title 👋 1.Install Dependencies, this could take a while.\n",
        "from IPython.utils import io\n",
        "import os\n",
        "import subprocess\n",
        "import tqdm.notebook\n",
        "\n",
        "TQDM_BAR_FORMAT = '{l_bar}{bar}| {n_fmt}/{total_fmt} [elapsed: {elapsed} remaining: {remaining}]'\n",
        "\n",
        "try:\n",
        "  with tqdm.notebook.tqdm(total=100, bar_format=TQDM_BAR_FORMAT) as pbar:\n",
        "    with io.capture_output() as captured:\n",
        "      # Uninstall default Colab version of TF.\n",
        "      !mkdir external-libraries &> /dev/null\n",
        "      ! wget https://repo.anaconda.com/miniconda/Miniconda3-py37_4.8.2-Linux-x86_64.sh &> /dev/null\n",
        "      ! chmod +x Miniconda3-py37_4.8.2-Linux-x86_64.sh &> /dev/null\n",
        "      ! bash ./Miniconda3-py37_4.8.2-Linux-x86_64.sh -b -f -p /home/aistudio/external-libraries &> /dev/null\n",
        "      #@title Install Pymol\n",
        "      pbar.update(6)\n",
        "      ! yes | /home/aistudio/external-libraries/bin/conda install -c conda-forge vmd &> /dev/null\n",
        "      #@title Install VMD\n",
        "      !yes | /home/aistudio/external-libraries/bin/conda install -c conda-forge pymol-open-source &> /dev/null\n",
        "      #install openmm\n",
        "      !yes | /home/aistudio/external-libraries/bin/conda install -c conda-forge openmm &> /dev/null\n",
        "      #install pdbfixer\n",
        "      pbar.update(2)\n",
        "      !yes | /home/aistudio/external-libraries/bin/conda install -c conda-forge pdbfixer &> /dev/null\n",
        "      #install visualization\n",
        "      pbar.update(9)\n",
        "      ! /home/aistudio/external-libraries/bin/pip install py3Dmol &> /dev/null\n",
        "      # pip install pdb-tools\n",
        "      ! /home/aistudio/external-libraries/bin/pip install pdb-tools &> /dev/null\n",
        "      #install visualization\n",
        "      !yes | /home/aistudio/external-libraries/bin/conda install -c rdkit rdkit &> /dev/null\n",
        "      #install csh shell for step 8 or step 9\n",
        "      pbar.update(80)\n",
        "      !apt-get install csh &> /dev/null\n",
        "      !apt-get install vim &> /dev/null\n",
        "      pbar.update(2)\n",
        "      import sys\n",
        "      sys.path.append('/home/aistudio/external-libraries/lib/python3.7/')\n",
        "      import sys\n",
        "      sys.path.append('/home/aistudio/external-libraries/lib/python3.7/site-packages/')\n",
        "      #import rdkit for visualization purpose\n",
        "      from rdkit import Chem\n",
        "      from rdkit.Chem import AllChem\n",
        "      from rdkit.Chem import Draw\n",
        "      from rdkit.Chem.Draw import IPythonConsole\n",
        "      from rdkit.Chem import DataStructs\n",
        "      from rdkit.Chem import RDConfig\n",
        "      from rdkit.Chem import rdBase\n",
        "      from rdkit.Chem import MolFromPDBFile\n",
        "      import py3Dmol\n",
        "      pbar.update(1)\n",
        "except subprocess.CalledProcessError:\n",
        "  print(captured)\n",
        "  raise"
      ],
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "ff68e344e05449dbb4784f425e0c6594",
              "version_minor": 0,
              "version_major": 2
            },
            "text/plain": [
              "  0%|          | 0/100 [elapsed: 00:00 remaining: ?]"
            ]
          },
          "metadata": {}
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 514
        },
        "id": "pp1LY8czS5Ru",
        "outputId": "e727474b-e71e-4efa-a8ee-e3cf83514f0d"
      },
      "source": [
        "#@title Check if your protein has multple chains\n",
        "import py3Dmol\n",
        "view = py3Dmol.view(query='pdb:4YLL')\n",
        "view.setStyle({'cartoon':{'color':'spectrum'}})\n",
        "view"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "application/3dmoljs_load.v0": "<div id=\"3dmolviewer_163628572089443\"  style=\"position: relative; width: 640px; height: 480px\">\n        <p id=\"3dmolwarning_163628572089443\" style=\"background-color:#ffcccc;color:black\">You appear to be running in JupyterLab (or JavaScript failed to load for some other reason).  You need to install the 3dmol extension: <br>\n        <tt>jupyter labextension install jupyterlab_3dmol</tt></p>\n        </div>\n<script>\n\nvar loadScriptAsync = function(uri){\n  return new Promise((resolve, reject) => {\n    var tag = document.createElement('script');\n    tag.src = uri;\n    tag.async = true;\n    tag.onload = () => {\n      resolve();\n    };\n  var firstScriptTag = document.getElementsByTagName('script')[0];\n  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);\n});\n};\n\nif(typeof $3Dmolpromise === 'undefined') {\n$3Dmolpromise = null;\n  $3Dmolpromise = loadScriptAsync('https://3dmol.org/build/3Dmol.js');\n}\n\nvar viewer_163628572089443 = null;\nvar warn = document.getElementById(\"3dmolwarning_163628572089443\");\nif(warn) {\n    warn.parentNode.removeChild(warn);\n}\n$3Dmolpromise.then(function() {\nviewer_163628572089443 = $3Dmol.createViewer($(\"#3dmolviewer_163628572089443\"),{backgroundColor:\"white\"});\n$3Dmol.download(\"pdb:4YLL\", viewer_163628572089443, {}, function() {\nviewer_163628572089443.zoomTo();\n\tviewer_163628572089443.setStyle({\"cartoon\": {\"color\": \"spectrum\"}});\nviewer_163628572089443.render();\n})\n});\n</script>",
            "text/html": [
              "<div id=\"3dmolviewer_163628572089443\"  style=\"position: relative; width: 640px; height: 480px\">\n",
              "        <p id=\"3dmolwarning_163628572089443\" style=\"background-color:#ffcccc;color:black\">You appear to be running in JupyterLab (or JavaScript failed to load for some other reason).  You need to install the 3dmol extension: <br>\n",
              "        <tt>jupyter labextension install jupyterlab_3dmol</tt></p>\n",
              "        </div>\n",
              "<script>\n",
              "\n",
              "var loadScriptAsync = function(uri){\n",
              "  return new Promise((resolve, reject) => {\n",
              "    var tag = document.createElement('script');\n",
              "    tag.src = uri;\n",
              "    tag.async = true;\n",
              "    tag.onload = () => {\n",
              "      resolve();\n",
              "    };\n",
              "  var firstScriptTag = document.getElementsByTagName('script')[0];\n",
              "  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);\n",
              "});\n",
              "};\n",
              "\n",
              "if(typeof $3Dmolpromise === 'undefined') {\n",
              "$3Dmolpromise = null;\n",
              "  $3Dmolpromise = loadScriptAsync('https://3dmol.org/build/3Dmol.js');\n",
              "}\n",
              "\n",
              "var viewer_163628572089443 = null;\n",
              "var warn = document.getElementById(\"3dmolwarning_163628572089443\");\n",
              "if(warn) {\n",
              "    warn.parentNode.removeChild(warn);\n",
              "}\n",
              "$3Dmolpromise.then(function() {\n",
              "viewer_163628572089443 = $3Dmol.createViewer($(\"#3dmolviewer_163628572089443\"),{backgroundColor:\"white\"});\n",
              "$3Dmol.download(\"pdb:4YLL\", viewer_163628572089443, {}, function() {\n",
              "viewer_163628572089443.zoomTo();\n",
              "\tviewer_163628572089443.setStyle({\"cartoon\": {\"color\": \"spectrum\"}});\n",
              "viewer_163628572089443.render();\n",
              "})\n",
              "});\n",
              "</script>"
            ]
          },
          "metadata": {}
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<py3Dmol.view at 0x7fad7b18cdd0>"
            ]
          },
          "metadata": {},
          "execution_count": 3
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "k4MHHzof6sYM"
      },
      "source": [
        "# Your PDB structure should NOT contain any broken backbones, otherwise this notebook will fail. Please use some third-party tools to fix it if there is any. You could use swiss-model as per https://quantaosun.github.io/gromacs.htm to generate a homology model for your protein. Alternatively, github/alphafold.ipynb may also helpful."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vjlb3i6L3zpo",
        "cellView": "form"
      },
      "source": [
        "#@title 👋 2A.Upload a PDB file with a single chain. \n",
        "PDB_path = \"6nzp_editv23\" #@param {type:\"string\"}\n",
        "#!/home/aistudio/external-libraries/bin/pdbfixer  '{PDB_path}' --ph=7  --replace-nonstandard --add-residues"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Vx0M_FXDp74w",
        "cellView": "form"
      },
      "source": [
        "#@title 👋 2B (Skip this if you finished 2A)Download the PDB file if you are sure this sturcture is ready to simualtion\n",
        "PDB_ID = \"3HTB\" #@param {type:\"string\"}\n",
        "pdb = PDB_ID + \".pdb\"\n",
        "! wget https://files.rcsb.org/download/$pdb"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "bAtKHO9H0wlX",
        "cellView": "form"
      },
      "source": [
        "#@title 👋 3.Spilit protien and ligand, protein_no_H.pdb and ligand_no_H.pdb should be generated.\n",
        "ligand_name = \"JZ4\" #@param {type:\"string\"}\n",
        "PDB_path = \"3htb.pdb\" #@param {type:\"string\"}\n",
        "!grep ATOM '{PDB_path}' > protein_no_H.pdb\n",
        "!grep HETATM '{PDB_path}' > ligand_1.pdb\n",
        "!grep '{ligand_name}' ligand_1.pdb > ligand_no_H.pdb"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "b32SN7VsWVwb"
      },
      "source": [
        "# ⚠️Upload ligand_no_H.pdb to MolProbity web server developed by Duke Universityhttp://molprobity.biochem.duke.edu/, add hydrogen, then download as ligand_with_H.pdb"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "1uVJRwrOPktm"
      },
      "source": [
        "# 👋 ⚠️4.Uplaod ligand_with_H.pdb to LigParGen to generate PDB,PRM and RTF three files. Then upload them here.\n",
        "http://zarbi.chem.yale.edu/ligpargen/index.html "
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "XML_OXOj1Jag",
        "outputId": "a83373d7-f918-46c2-8ec7-94ec07965ffd",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "#@title 👋 ⚠️ 5. Combine protien_no_H.pdb and new ligand PDB from LigPargen, a new \"complex.pdb\" should be created.\n",
        "com_file = open('combine_protien_ligand.pml','w')\n",
        "com_file.write('''\n",
        "load protein.pdb\n",
        "load UNK_781C42.pdb\n",
        "select all\n",
        "save complex.pdb, all \n",
        "''')\n",
        "com_file.close()\n",
        "!/home/aistudio/external-libraries/bin/pymol -c combine_protien_ligand.pml"
      ],
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            " PyMOL(TM) Molecular Graphics System, Version 2.5.0.\n",
            " Copyright (c) Schrodinger, LLC.\n",
            " All Rights Reserved.\n",
            " \n",
            "    Created by Warren L. DeLano, Ph.D. \n",
            " \n",
            "    PyMOL is user-supported open-source software.  Although some versions\n",
            "    are freely available, PyMOL is not in the public domain.\n",
            " \n",
            "    If PyMOL is helpful in your work or study, then please volunteer \n",
            "    support for our ongoing efforts to create open and affordable scientific\n",
            "    software by purchasing a PyMOL Maintenance and/or Support subscription.\n",
            "\n",
            "    More information can be found at \"http://www.pymol.org\".\n",
            " \n",
            "    Enter \"help\" for a list of commands.\n",
            "    Enter \"help <command-name>\" for information on a specific command.\n",
            "\n",
            " Hit ESC anytime to toggle between text and graphics.\n",
            "\n",
            " Detected 2 CPU cores.  Enabled multithreaded rendering.\n",
            "PyMOL>load protein.pdb\n",
            " Warning: 'protein' may become a reserved selection keyword in the future\n",
            "HEADER    TRANSPORT PROTEIN                       25-NOV-17   5YVE\n",
            "TITLE     CRYSTAL STRUCTURE OF HUMAN P2X3 RECEPTOR IN COMPLEX WITH THE\n",
            "TITLE    2 AF-219 NEGATIVE ALLOSTERIC MODULATOR\n",
            " ObjectMolecule: Read crystal symmetry information.\n",
            " CmdLoad: \"\" loaded as \"protein\".\n",
            "PyMOL>load UNK_781C42.pdb\n",
            " CmdLoad: \"\" loaded as \"UNK_781C42\".\n",
            "PyMOL>select all\n",
            " Selector: selection \"sele\" defined with 15510 atoms.\n",
            "PyMOL>save complex.pdb, all\n",
            " Save: wrote \"complex.pdb\".\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "A2IY5UQy4s66"
      },
      "source": [
        "#@title 6 👋Download Charmm Force Field\n",
        "!wget https://raw.githubusercontent.com/quantaosun/NAMD-MD/main/top_all36_prot.prm \n",
        "!wget https://raw.githubusercontent.com/quantaosun/NAMD-MD/main/top_all36_prot.rtf \n",
        "!wget https://raw.githubusercontent.com/quantaosun/NAMD-MD/main/toppar_water_ions.mod.str"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "l0nNG3B81N2_"
      },
      "source": [
        "# Next part is extremely tricky, we need to use VMD to generate PSF and a new PDB file for complex.pdb, the PSF and PDB then will be taken to NAMD simulation. An ideal PDB file for complex.pdb should not contain any \"CONNECT\" keyword, and protein ligand separated by a \"TER\" keyword, pymol is a good option to combine two PDB file. Always double check the final complex.pdb you decide to put into vmd, to make sure there is no unnecessary section inside even it looks good as a 3D object in any visualization software."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "P2JHMSJuYxqw"
      },
      "source": [
        "# 7.  Refer to http://zarbi.chem.yale.edu/ligpargen/namd_tutorial.html to use local VMD to produce the \"ionized.pdb\" and \"ionized.psf\" Double check your files actually contains your small molecule. "
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "bSqaesfTcQvw",
        "cellView": "form"
      },
      "source": [
        "#@title 👋 ⚠️7A (Skip this if you have finished step 7, this step currently is problematic)Start VMD command line, type line by line as listed below,make sure CHANGE ligand topolog to what you have downloaded from LigParGen \n",
        "%cd /content\n",
        "!mkdir psfgen\n",
        "%cd psfgen\n",
        "!cp /content/complex.pdb ./\n",
        "!cp /content/JZ4_1030E0.rtf ./\n",
        "!cp /content/top_all36_prot.rtf ./\n",
        "!/home/aistudio/external-libraries/bin/vmd "
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "59d-U1nbdBqZ"
      },
      "source": [
        "#psfgen for a trimer protein with a ligand bound\n",
        "mol delete all\n",
        "mol load pdb ../complex.pdb\n",
        "set bad [atomselect top \"resname ACE\"]\n",
        "if {[info exists bad]} {\n",
        "set chainA [atomselect top \"chain A and not hydrogen and not resname ACE NME\"]\n",
        "set chainC [atomselect top \"chain C and not hydrogen and not resname ACE NME\"]\n",
        "set chainB [atomselect top \"chain B and not hydrogen and not resname ACE NME\"]\n",
        "set chainX [atomselect top \"residuetype nothing and not resname ACE NME\"]\n",
        "set flag 1\n",
        "} else {\n",
        "set chainA [atomselect top \"chain A and not hydrogen\"]\n",
        "set chainC [atomselect top \"chain C and not hydrogen\"]\n",
        "set chainB [atomselect top \"chain B and not hydrogen\"]\n",
        "set chainX [atomselect top \"residuetype nothing\"]\n",
        "set flag 0}\n",
        "$chainA writepdb chainA.pdb\n",
        "$chainC writepdb chainC.pdb\n",
        "$chainB writepdb chainB.pdb\n",
        "$chainX writepdb chainX.pdb\n",
        "package require psfgen\n",
        "topology ../top_all36_prot.rtf\n",
        "topology ../5_reference.rtf\n",
        "pdbalias HIS HSD\n",
        "pdbalias atom SER HG HG1\n",
        "pdbalias residue HIS HSE\n",
        "pdbalias atom ILE CD1 CD\n",
        "if {$flag == 1} {\n",
        "segment A {\n",
        "  first ACE\n",
        "  last CT3\n",
        "  pdb chainA.pdb\n",
        "}\n",
        "segment C {\n",
        "  first ACE\n",
        "  last CT3\n",
        "  pdb chainC.pdb\n",
        "}\n",
        "segment B {\n",
        "  first ACE\n",
        "  last CT3\n",
        "  pdb chainB.pdb\n",
        "}\n",
        "} else {\n",
        "segment A {\n",
        "  first NONE\n",
        "  last NONE\n",
        "  pdb chainA.pdb\n",
        "}\n",
        "segment C {\n",
        "  first NONE\n",
        "  last NONE\n",
        "  pdb chainC.pdb\n",
        "}\n",
        "segment B {\n",
        "  first NONE\n",
        "  last NONE\n",
        "  pdb chainB.pdb\n",
        "}\n",
        "}\n",
        "segment X {\n",
        "  first NONE\n",
        "  last NONE\n",
        "  pdb chainX.pdb\n",
        "}\n",
        "coordpdb chainA.pdb A\n",
        "coordpdb chainC.pdb C\n",
        "coordpdb chainB.pdb B\n",
        "coordpdb chainX.pdb X\n",
        "guesscoord\n",
        "regenerate angles dihedrals\n",
        "writepdb psf-complex.pdb\n",
        "writepsf psf-complex.psf\n",
        "exit "
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7udU85rG9kiM"
      },
      "source": [
        "#psfgen for a single-chained protein with a ligand bound\n",
        "mol delete all\n",
        "mol load pdb ../complex.pdb\n",
        "set bad [atomselect top \"resname ACE\"]\n",
        "if {[info exists bad]} {\n",
        "set chainB [atomselect top \"chain B and not hydrogen and not resname ACE NME\"]\n",
        "set chainX [atomselect top \"residuetype nothing and not resname ACE NME\"]\n",
        "set flag 1\n",
        "} else {\n",
        "set chainB [atomselect top \"chain B and not hydrogen\"]\n",
        "set chainX [atomselect top \"residuetype nothing\"]\n",
        "set flag 0}\n",
        "$chainB writepdb chainB.pdb\n",
        "$chainX writepdb chainX.pdb\n",
        "package require psfgen\n",
        "topology ../top_opls_aam.inp\n",
        "topology ../ligand.rtf\n",
        "pdbalias HIS HSD\n",
        "pdbalias atom SER HG HG1\n",
        "pdbalias residue HIS HSE\n",
        "pdbalias atom ILE CD1 CD\n",
        "if {$flag == 1} {\n",
        "segment B {\n",
        "  first ACE\n",
        "  last CT3\n",
        "  pdb chainB.pdb\n",
        "}\n",
        "} else {\n",
        "segment B {\n",
        "  first NONE\n",
        "  last NONE\n",
        "  pdb chainB.pdb\n",
        "}\n",
        "}\n",
        "segment X {\n",
        "  first NONE\n",
        "  last NONE\n",
        "  pdb chainX.pdb\n",
        "}\n",
        "coordpdb chainB.pdb B\n",
        "coordpdb chainX.pdb X\n",
        "guesscoord\n",
        "regenerate angles dihedrals\n",
        "writepdb psf-complex.pdb\n",
        "writepsf psf-complex.psf\n",
        "exit"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "twJ_-eqypnjb"
      },
      "source": [
        "#If you want to add membrane\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "xkHOL7coQP6N"
      },
      "source": [
        "#Load your membrane: VMD Main-File-New Molecule-Browse(popc(pore_r=24).pdb psf） https://www.charmm-gui.org/archive/bilayer/popc.tar.gz\n",
        "#generate popc_autopsf.pdb psf: Extensions-Modeling-Automatic PSF Builder-Load input files(top_all36_cgenff.rtf & par_all36_lipid.prm)-Guess and split..-Create chains(without water)\n",
        "#generate complex_popc_raw.pdb psf\n",
        "set popc [atomselect top all]\n",
        "set kcsamol [mol load psf psf-complex.psf pdb psf-complex.pdb]\n",
        "set kcsa [atomselect $kcsamol all]\n",
        "$popc moveby [vecinvert [measure center $popc weight mass]] #may optioned\n",
        "$popc writepdb popc_TEMP.pdb\n",
        "set vest [atomselect $kcsamol \"protein and resid 327 to 333\"] #the number of residues is up to your protein\n",
        "$kcsa moveby [vecinvert [measure center $vest weight mass]]\n",
        "display resetview\n",
        "$kcsa move [transaxis z -25] #Rotate around the z axis\n",
        "$kcsa writepdb kcsa_TEMP.pdb\n",
        "mol delete all\n",
        "package require psfgen\n",
        "resetpsf\n",
        "readpsf popc_autopsf.psf #name of your memrane psf file\n",
        "coordpdb popc_TEMP.pdb\n",
        "readpsf psf-complex.psf\n",
        "coordpdb kcsa_TEMP.pdb\n",
        "writepsf complex_popc.psf\n",
        "writepdb complex_popc.pdb\n",
        "file delete kcsa_TEMP.pdb\n",
        "file delete popc_TEMP.pdb"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "OHqYxy82qNMv"
      },
      "source": [
        "#Solvate and add ions to membrane_system\n",
        "package require solvate  \n",
        "solvate complex_popc.psf complex_popc.pdb -t 15 -o complex_wb \n",
        "package require autoionize\n",
        "autoionize -psf complex_wb.psf -pdb complex_wb.pdb -sc 0.15 -cation SOD -o ionized\n",
        "set ubq [atomselect top all]\n",
        "measure minmax $ubq   \n",
        "measure center $ubq "
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ez1tejkQrMSF"
      },
      "source": [
        "#Solvate and add ions to system\n",
        "package require solvate  \n",
        "solvate psf-complex.psf psf-complex.pdb -t 15 -o complex_wb \n",
        "package require autoionize\n",
        "autoionize -psf complex_wb.psf -pdb complex_wb.pdb -sc 0.15 -cation SOD -o ionized\n",
        "set ubq [atomselect top all]\n",
        "measure minmax $ubq   \n",
        "measure center $ubq "
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "kkTNPaSduwHL",
        "cellView": "form"
      },
      "source": [
        "#@title 👋 ⚠️8.Download NAMD confiles and then manually modify the ligand parameter path, and modify PBC conditions based on water box dimensions outputed from previous step.\n",
        "\n",
        "!wget https://raw.githubusercontent.com/quantaosun/NAMD-MD/main/nvt.namd \n",
        "!wget https://raw.githubusercontent.com/quantaosun/NAMD-MD/main/npt.namd\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "XBYVxQ6Knb50"
      },
      "source": [
        "#use this to measure the box dimension and center.\n",
        "set ubq [atomselect top all]\n",
        "measure minmax $ubq   \n",
        "measure center $ubq "
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "_Masmsbysn_K"
      },
      "source": [
        "[链接文字](`https://`)Modify configuration file\n",
        "![Screen Shot 2021-10-23 at 6.26.03 PM.png]()\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "fbjsJJyLl0u7",
        "cellView": "form"
      },
      "source": [
        "#@title 👋 9.Import NAMD software. You are supposed to have the NAMD software.\n",
        "NAMD_tarball_PATH = \"/content/drive/MyDrive/softwares/NAMD_3.0alpha8_Linux-x86_64-multicore-CUDA-SingleNode.tar.gz\" #@param {type:\"string\"}\n",
        "\n",
        "!tar -xvf '{NAMD_tarball_PATH}' &> /dev/null\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "bRB2815xNyXi",
        "cellView": "form"
      },
      "source": [
        "#@title 👋 10.Run the Simulation and Copy result back to Goolge Drive\n",
        "Path = \"/content/drive/MyDrive/NAMD-MD\" #@param {type:\"string\"}\n",
        "\n",
        "!/content/NAMD_3.0alpha8_Linux-x86_64-multicore-CUDA/namd3 +p2 nvt.namd > nvt.log\n",
        "#!/content/NAMD_3.0alpha8_Linux-x86_64-multicore-CUDA/namd3 +p2 npt.namd > npt.log &&\n"
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}